﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows.Forms;

namespace xDataSet
{
    public class Ref
    {
        public static object GetInstance(string pModel , string pClass , string pVersion)
        {
            string _FileName = pModel + ".dll";
            if (!File.Exists(_FileName))
            {
                MessageBox.Show("指定的文件不存在" + "\r\n"  + _FileName);
                return null;
            }
            System.Reflection.Assembly _DllName;
            byte[] b = File.ReadAllBytes(_FileName);
            _DllName = System.Reflection.Assembly.Load(b);
            Type t_Type = _DllName.GetType(pModel + "." + pClass);
            //类定义
            return System.Activator.CreateInstance(t_Type, new object[] { new SdkClass("X","1")});

        }

        public static object DoMethod(string pMethod , string pParamters, object pAssemblyObject)
        {
            try
            {
                //定义一个成员信息类对象数组,并从程序集中获取.
                MethodInfo mMethod_Get_Info = null;
                try
                {
                    mMethod_Get_Info = pAssemblyObject.GetType().GetMethod(pMethod);
                    //没有找到该方法，就抛出异常
                    if (mMethod_Get_Info == null)
                    {
                        return null;
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show( "寻找调用DLL方法：" + pMethod + ",调用失败，未能找到该方法，信息：" + ex.Message);
                    throw ex;
                }
                //声明参数
                object[] arg = new object[1] { pParamters };
                string value = "";
                //直接调用
                try
                {
                    value = mMethod_Get_Info.Invoke(pAssemblyObject, arg).ToString();
                }
                catch (Exception ex)
                {
                    MessageBox.Show ("执行调用DLL方法：" + pMethod + ",执行失败，信息：" + ex.Message);
                    throw ex;
                }
                return value;
            }
            catch (Exception ex)
            {
                //出错信息
                throw ex;
            }

        }


       
        
    }
}
